package com.chx.three.mama.fragments;


import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.chx.three.mama.activities.ActionDetailActivity;
import com.chx.three.mama.R;
import com.chx.three.mama.activities.MyGifActivity;
import com.chx.three.mama.adapter.ActionAdapter;
import com.chx.three.mama.model.ActionList;
import com.chx.three.mama.task.ActionListTask;
import com.chx.three.mama.utils.TaskProcessor;
import com.chx.three.mama.view.Topbar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 活动页面
 */
public class ActionFragment extends Fragment implements TaskProcessor, AdapterView.OnItemClickListener, Topbar.onTopbarClickListener {
    private List<ActionList> mdata;
    private ActionAdapter madapter;
    private RelativeLayout mLayout;
    private ListView mListView;
    private Topbar topbar;

    public ActionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_action, container, false);

        mLayout = (RelativeLayout) ret.findViewById(R.id.anim_layout);

        topbar = (Topbar) ret.findViewById(R.id.topbar);
        topbar.setOnTopbarClickListener(this);

        ImageView mLoadImg = (ImageView) ret.findViewById(R.id.loading_img);
        Drawable mBackGround = mLoadImg.getBackground();
        if(mBackGround instanceof AnimationDrawable){
            AnimationDrawable mAnim = (AnimationDrawable)mBackGround;
            mAnim.start();
        }

        ActionListTask mTask = new ActionListTask(this);
        mTask.execute();

        mListView = (ListView) ret.findViewById(R.id.action_list);
        mdata = new ArrayList<>();
        if (mListView != null) {
            madapter = new ActionAdapter(getActivity(), mdata);
            mListView.setAdapter(madapter);
            mListView.setOnItemClickListener(this);
        }
        return ret;
    }


    @Override
    public void processResult(JSONObject result) {
        if (result != null) {

            try {

                JSONArray mArray = result.getJSONArray("data");
                for (int i = 0; i < mArray.length(); i++) {
                    JSONObject mObject = mArray.getJSONObject(i);
                    ActionList mActionList = new ActionList();
                    mActionList.parseJson(mObject);
                    mdata.add(mActionList);
                }
                madapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mLayout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ActionList mActionList = mdata.get(position);
        String mUrl = mActionList.getUrl();
        Intent intent = new Intent(getActivity(), ActionDetailActivity.class);
        intent.putExtra("url",mUrl);
        startActivity(intent);
    }

    @Override
    public void onLeftButtonClick() {

    }

    @Override
    public void onRightButtonClick() {
        Intent intent = new Intent(getActivity(), MyGifActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSearchButtonClick() {

    }
}
